package eu.sudparis.it.opp.observfram;
import java.util.Set;

import eu.sudparis.it.opp.Node;
import eu.sudparis.it.opp.TimeSeries;
import eu.sudparis.it.opp.graph.EgoNetwork;
import eu.sudparis.it.opp.graph.Snapshot;

public class ObserverNode extends Node{
	//public Observation global;
	public EgoNetwork local;
	public TimeSeries series;
	public ObserverNode(int id, TimeSeries ts){
		super.id=id;
		this.local=new EgoNetwork();
		this.local.bind=this;
		this.series=ts;
		Node nd=new Node();
		nd.wrap(this, series.graph);
	}
	public void encounter(Node nd) {
		super.encounter(nd);
		this.local.observeEncounter((ObserverNode)nd);
		((ObserverNode) nd).local.observeEncounter(this);
		if(super.chain_of_responsibility!=null)
			super.chain_of_responsibility.encounter(nd);
	}
	
	public void syncKnow(Node nd, Set<Node> trace, Snapshot snap) {
		if (trace.contains(nd))
			return;
		trace.add(nd);
		
		for (Node ndd : inContact) {
			((ObserverNode)ndd).syncKnow(this,trace,snap);
		}
		for (Node ndd : nd.inContact) {
			((ObserverNode)ndd).syncKnow(nd,trace,snap);
		}
	}

	@Override
	public void leaveWith(Node nd) {
		// TODO Auto-generated method stub
		this.local.observeLeave((ObserverNode)nd);
		((ObserverNode)nd).local.observeLeave(this);
		if(super.chain_of_responsibility!=null)
			super.chain_of_responsibility.leaveWith(nd);
		super.leaveWith(nd);
	}
	@Override
	public ObserverNode clone() {
		// TODO Auto-generated method stub
		return new ObserverNode(-1,this.series);
	}
	@Override
	public void communicate(Set<Node> nods) {
		// TODO Auto-generated method stub
		if(this.chain_of_responsibility!=null)
			this.chain_of_responsibility.communicate(nods);
	}
}
